﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleAppAsyncEnumeratorCore
{
    class Program
    {
        private static readonly CancellationTokenSource cts =new CancellationTokenSource();
        static async Task Main(string[] args)
        {
            MyParam x = new MyParam();
           var c= x.ToString();
            Task.Run(() =>
            {
                Thread.Sleep(5000);
                cts.Cancel();
            });
            await foreach(var dataPoint in FetchIotDataAsync().WithCancellation(cts.Token))
            {
                if (!cts.IsCancellationRequested)
                {
                    Console.WriteLine(dataPoint);
                }
                else
                {
                    Console.WriteLine("IsCanceled");
                    break;
                }
            }

            Console.ReadLine();
        }

        static async IAsyncEnumerable<int> FetchIotDataAsync()
        {
            for (int i = 1; i <= 10; i++)
            {
                await Task.Delay(2000);
                yield return 1;
            }
        }

        static  IEnumerable<int> FetchIotData()
        {
            for (int i = 1; i <= 10; i++)
            {
                Thread.Sleep(2000);
                yield return 1;
            }
        }
    }

    struct MyParam
    {
        public int number { get; set; }
    }
}
